//
//  WmcTeamCloudMemberDetailVM.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/14.
//

import Foundation
import RxRelay

class WmcTeamCloudMemberDetailVM: WmcBaseVM {
    /// 团队相册
    var teamAblum: WmcMediaTeamAlbumModel?
    /// 成员
    var teamMember: WmcTeamMemberModel?
    /// 成员详情
    let memberDetail: BehaviorRelay<WmcTeamMemberDetailModel?> = .init(value: nil)

    // Internal

    enum DataType {
        /// 全部照片
        case allPhoto
        /// 水印照片
        case watermarkPhoto
    }

    /// 刷新数据
    var dataNeedUpdate: (() -> Void)?

    /// 水印类型请求参数
    var watermarkId: Int?

    /// 页面数据类型
    var type: DataType = .allPhoto
}

extension WmcTeamCloudMemberDetailVM {
    /// 获取自己作为成员的模型
    /// - Returns: description
    func getSelfMember(succeed: ((WmcTeamMemberModel) -> Void)?) {
        guard let teamId = teamAblum?.teamId else {
            return
        }
        WmcHudManager.loading()
        WmcMediaTeamManagerAPI.fetchMemberList(id: teamId).subscribe { list in
            WmcHudManager.dismiss()
            list.forEach { member in
                if member.isSelf() {
                    guard let succeed = succeed else {
                        return
                    }
                    succeed(member)
                }
            }
        } onError: { error in
            WmcHudManager.dismiss()
            WmcHudManager.toast(error)
        }.disposed(by: rx.disposeBag)
    }
}

extension WmcTeamCloudMemberDetailVM {
    func getMemberInfo() {
        guard let teamId = teamAblum?.teamId, let memeberId = teamMember?.memberId, let yupaoId = teamMember?.uid else { return }
        WmcHudManager.loading()
        WmcMediaTeamManagerAPI.fetchMemberInfo(teamId: teamId, memberId: memeberId, yupaoId: yupaoId).subscribe { [weak self] detail in
            guard let weakSelf = self else { return }
            weakSelf.memberDetail.accept(detail)
            WmcHudManager.dismiss()
        } onError: { error in
            WmcHudManager.dismiss()
            WmcHudManager.toast(error)
        }.disposed(by: rx.disposeBag)
    }
}
